package com.shj.em.biz.sell.controller;

import com.shj.em.biz.sys.service.CdService;
import com.shj.em.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.sell.entity.QuotItem;
import com.shj.em.biz.sell.service.QuotItemService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 报价信息 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-07
 */
@RestController
@Slf4j
@RequestMapping("/sell/quot/quotItem")
public class QuotItemController extends BaseController<QuotItem>{

    @Autowired
    QuotItemService quotitemService;

    @Autowired
    CdService cdService;

    @RequestMapping("/list")
    public PageVO<QuotItem> list(@RequestBody Map<String, Object> params){
        PageVO<QuotItem> pageVO = new PageVO<>();
        Page<QuotItem> page = quotitemService.queryQuotItemPage(params);
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @GetMapping("/findQuotItemsByQuotId/{quotId}")
    public ResMsg findQuotItemsByQuotId(@PathVariable long quotId){
        List<QuotItem> items = quotitemService.queryQuotItemsByQuotId(quotId);
        return ResMsg.ok(items);
    }


    @Log("创建产品信息")
    @PostMapping("/add")
    public ResMsg<String> add(@RequestBody QuotItem quotitem){
        quotitemService.insert(quotitem);
        return ResMsg.ok();
    }

    @Log("更新产品信息")
    @PostMapping("/edit")
    public ResMsg edit(@RequestBody QuotItem quotitem){
        quotitemService.updateById(quotitem);
        return ResMsg.ok();
    }

    @Log("批量删除报价产品")
    @DeleteMapping( "/batchRemove/{ids}")
    public ResMsg batchRemove(@PathVariable("ids") String ids){
        quotitemService.deleteBatchIds(Arrays.asList(ids.split(",")));
        return ResMsg.ok();
    }






    /*@GetMapping("/gotoAdd/{quotId}")
	String gotoAdd(@PathVariable Long quotId, Model model){
        QuotItem item = new QuotItem();
        item.setQuotId(quotId);
        model.addAttribute("isEdit", false);
        model.addAttribute("quotitem", item);
        addOtherInfo(model);
	    return prefix + "/add";
	}

	@GetMapping("/gotoEdit/{id}")
	String gotoEdit(@PathVariable("id") Long id, Model model){
        QuotItem quotitem = quotitemService.selectById(id);
		model.addAttribute("quotitem", quotitem);
        model.addAttribute("isEdit", true);
        addOtherInfo(model);
	    return prefix + "/add";
	}*/

	/*private void addOtherInfo(Model model){
        List<Cd> units = cdService.queryCdsByType(CdService.CD_TYPE_UNIT);
        model.addAttribute("units", units);
    }
*/




    @Log("删除报价产品")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id, @RequestParam("quotId") Long quotId){
        try{
            quotitemService.deleteById(id);
            BigDecimal total = quotitemService.getTotalPriceByQuotId(quotId);
            return ResMsg.ok(StringUtils.convertAmount2String(total,2));
        }catch(Exception e){
            return ResMsg.error();
        }
	}



}

